//
//  WJQAlertView.m
//  TourPlay
//
//  Created by dllo on 15/11/21.
//  Copyright © 2015年 王军岐. All rights reserved.
//

#import "WJQAlertView.h"
#import "WJQAlertCell.h"

#define WIDTH [[UIScreen mainScreen] bounds].size.width * (1 / 9.f)
#define HEIGHT [[UIScreen mainScreen] bounds].size.height * (1 / 4.f)
#define CELLHEIGHT 60.f * AUTOSCREEN_HEIGHT


@interface WJQAlertView () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) UITableView *myTableV;
@property (nonatomic, retain) UIView *view;
@property (nonatomic, retain) NSMutableArray *isHiddenCheckArray;
@property (nonatomic, retain) WJQAlertCell *cell;

@end

@implementation WJQAlertView
- (void)dealloc
{
    [_listArray release];
    [_listTitle release];
    [_myTableV release];
    [_view release];
    [_isHiddenCheckArray release];
    [_cell release];
    [super dealloc];
}

+ (instancetype)wjqAlertViewWithTitle:(NSString *)title;
{
    WJQAlertView *aler = [[[WJQAlertView alloc] initWithFrame:[[UIScreen mainScreen] bounds] title:title] autorelease];
    return aler;
}


- (instancetype)initWithFrame:(CGRect)frame title:(NSString *)title
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createSubViewsWithtitle:title];
    }
    return self;
}
- (void)createSubViewsWithtitle:(NSString *)title
{
    
    self.backgroundColor = [UIColor clearColor];
    self.isHiddenCheckArray = [NSMutableArray array];
    
    
    UIView *backgroundView = [[UIView alloc] initWithFrame:self.bounds];
    backgroundView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
    [self addSubview:backgroundView];
    [backgroundView release];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)];
    [backgroundView addGestureRecognizer:tap];
    [tap release];
    
    
    
    
    self.view = [[UIView alloc] initWithFrame:CGRectMake(self.frame.origin.x + WIDTH, HEIGHT, self.frame.size.width - WIDTH * 2, SCREEN_HEIGHT * (4 / 7.f))];
    self.view.layer.cornerRadius = 4.f;
    [self addSubview:self.view];
    [_view release];
    
    [self addColorChangedBlock:^{
        
        self.view.nightBackgroundColor = [ColorSingleton shareSingleton].cellColor;
        self.view.normalBackgroundColor = [UIColor whiteColor];
    }];
    
    
    self.listTitle = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height * (1 / 6.f))];
    self.listTitle.text = title;
    self.listTitle.textAlignment = NSTextAlignmentCenter;
    self.listTitle.font = [UIFont systemFontOfSize:14.f];
    self.listTitle.textColor = [UIColor colorWithRed:0.72 green:0.69 blue:0.7 alpha:1];
//    self.listTitle.backgroundColor = [UIColor orangeColor];
    [self.view addSubview:self.listTitle];
    [_listTitle release];

    

    
    // 关键帧动画, 改变view缩放属性
    CAKeyframeAnimation *keyAnimtion = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale"];
    // 设置关键帧的值, values是数组类型
    keyAnimtion.values = @[@(0.8), @(1.05), @(1.1), @(1)];
    keyAnimtion.duration = 0.3;
    [self.view.layer addAnimation:keyAnimtion forKey:@"key"];
    
    [[UIApplication sharedApplication].keyWindow addSubview:self];
  
}
- (void)tapAction
{
    self.hidden = YES;
} 
- (void)Show
{
    self.hidden = NO;
}
- (void)Hidden
{
    self.hidden = YES;
}
- (UITableView *)myTableV
{
    if (nil == _myTableV) {
        self.myTableV = [[UITableView alloc] initWithFrame:CGRectMake(WIDTH, self.listTitle.frame.size.height, self.view.frame.size.width - WIDTH * (2 / 3.f) - WIDTH, self.view.frame.size.height - self.listTitle.frame.size.height - 10) style:UITableViewStylePlain];
        self.myTableV.backgroundColor = [UIColor clearColor];
        self.myTableV.delegate = self;
        self.myTableV.dataSource = self;
        self.myTableV.separatorStyle = UITableViewCellSeparatorStyleNone;
        self.myTableV.userInteractionEnabled = YES;
        [self.myTableV registerClass:[WJQAlertCell class] forCellReuseIdentifier:@"cell"];
        
        [self.view addSubview:self.myTableV];
        [_myTableV release];

    }
    return _myTableV;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return CELLHEIGHT;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.listArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellStr = @"cell";
    self.cell = [tableView dequeueReusableCellWithIdentifier:cellStr];
    self.cell.cellLabel.text = [self.listArray objectAtIndex:indexPath.row];
    
    self.cell.backgroundColor = [UIColor clearColor];
    
    
    [self.cell checkWithisHidden:[self.isHiddenCheckArray objectAtIndex:indexPath.row]];
    

    
    return _cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    
    
    for (NSInteger i = 0; i < self.listArray.count; i++) {
        [self.isHiddenCheckArray replaceObjectAtIndex:i withObject:@"1"];
    }
//    NSLog(@"%ld", indexPath.row);
    
//    int i = ![[self.isHiddenCheckArray objectAtIndex:indexPath.row] intValue];
    
    if ([[self.isHiddenCheckArray objectAtIndex:indexPath.row] isEqualToString:@"0"]) {
        [self.isHiddenCheckArray replaceObjectAtIndex:indexPath.row withObject:@"1"];
    } else {
        [self.isHiddenCheckArray replaceObjectAtIndex:indexPath.row withObject:@"0"];
    }
    [self.myTableV reloadData];
    

    [self.delegate goToRowWithIndexPath:indexPath];

}
- (void)setListViewWithArray:(NSMutableArray *)listArray Row:(NSInteger)row
{
    CGRect rect = self.view.frame;
    
    if (self.listArray.count * 50.f < rect.size.height) {
        rect.size.height = self.listArray.count * 50.f + self.listTitle.frame.size.height + 10;
        
        CGRect rectTable = self.myTableV.frame;
        rectTable.size.height = self.listArray.count * 50.f;
        
        self.myTableV.frame = rectTable;
    }
    self.view.frame = rect;
    
    if (0 == self.isHiddenCheckArray.count) {

        for (NSInteger i = 0; i < self.listArray.count; i++) {
            if (i == row) {
                [self.isHiddenCheckArray addObject:@"0"];
            } else {
                [self.isHiddenCheckArray addObject:@"1"];
            }
        }
    }
    
    
    [self.myTableV reloadData];
    NSIndexPath *index = [NSIndexPath indexPathForRow:row inSection:0];
    [self.myTableV scrollToRowAtIndexPath:index atScrollPosition:UITableViewScrollPositionMiddle animated:NO];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
